<?php

namespace App\Http\Controllers\API\Admin;

use App\Http\Requests\CreateCategoryRequest;
use App\Http\Requests\UpdateCategoryRequest;
use App\Models\BonusCategory;
use App\Models\Category;
use App\Models\Navigate;
use App\Repositories\BonusRepository;
use App\Repositories\CategoryRepository;
use App\Http\Controllers\AppBaseController;
use App\Repositories\NavigateRepository;
use App\Repositories\PicturesRepository;
use App\ZL\Controllers\ApiBaseController;
use Illuminate\Http\Request;
use Flash;
use Illuminate\Support\Facades\Auth;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

class BonusCategoryAPIAdminController extends ApiBaseController
{
    private $bonusRepository;
    protected $model_name = 'App\Models\BonusCategory';
    protected $model_load_array = [
    ];
    protected $search_field_array = [
    ];

    public function __construct(BonusRepository $bonusRepository)
    {
        $this->bonusRepository = $bonusRepository;
    }


    public function authLimit()
    {
        $shop = Auth::guard('admin')->user()->shop;
        $limit = [];
        $limit[] = ['shop_id', $shop->id];
        return $limit;
    }

    public function beforeStore($id, $request)
    {
        $shop = Auth::guard('admin')->user()->shop;
        $model = $this->getModel();
        $obj = $model->find($id);
        $obj->shop_id = $shop->id;
        $obj->save();
        return responseZK(1,$obj);
    }

    public function beforeUpdate($id, $request)
    {
        $shop = Auth::guard('admin')->user()->shop;
        $model = $this->getModel();
        $obj = $model->find($id);
        $obj->shop_id = $shop->id;
        $obj->save();
        return responseZK(1,$obj);
    }

    public function distribute(Request $request)
    {
        $input = $request->all();
        $input['user_id'];
        $input['id'];
        $input['number'];
        $models = [];
        for($i=0;$i<$input['number'];$i++){
            $cate = BonusCategory::find($input['id']);
            if($cate){
                $_input = [
                  'bonuse_category_id'=>$cate->id,
                  'shop_id'=>$cate->shop_id,
                  'name'=>$cate->name,
                  'user_id'=>$input['user_id'],
                  'distribute_id'=>Auth::id(),
                  'money'=>$cate->money,
                  'type'=>$cate->type,
                  'min_amounte'=>$cate->min_amounte,
                  'max_amounte'=>$cate->max_amounte,
                  'use_start_date'=>$input['use_start_date'],
                  'use_end_date'=>$input['use_end_date'],
                ];
            }
            $_input['sn'] = date('YmdHis').mt_rand(1000,9999);
            $model = $this->bonusRepository->create($_input);
            $models[] = $model;
        }
        return responseZK(1,$models);
    }
}
